package cbaServer;

import builderProduct.FileProduct;
import commonSources.Cliente;
import commonSources.Funcionario;
import commonSources.Painel;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import operacoes2.Levantamento;
import operacoes2.Operacao;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.ABEDC692-9565-DD73-7D31-F81407E31382]
// </editor-fold> 
public class FileHandler {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0FDF6AC4-E2C8-E38E-3280-613E08E62D5E]
    // </editor-fold> 
    private File clientFile;
    private File panelsFile;
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.02BF05E1-D519-ACC0-DE22-B0E5CF569C19]
    // </editor-fold> 
    private final String fileClientes = "clientes.dat";
    private final String fileFunc = "funcionarios.dat";
    private final String filePanels = "paineis.dat";
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private static FileHandler fileHandler;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.43BFFCAC-4D09-272D-451F-2818F935D078]
    // </editor-fold> 
    private FileHandler() {
        this.clientFile = new File(fileClientes);
        this.panelsFile = new File(filePanels);
    }

    public static FileHandler getInstance() {
        if (fileHandler == null) {
            fileHandler = new FileHandler();
        }
        return fileHandler;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.D464F6BD-636D-2621-742F-1EB14B319894]
    // </editor-fold>
    public synchronized void saveBinaryFile(Vector<Operacao> val) {
        try {
            out = new ObjectOutputStream(new FileOutputStream(new File("OPREL" + getDateTime() + ".rel")));
            out.writeObject(val);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized void saveFileProduct(FileProduct fb, Vector<Operacao> val) {
        System.out.println("SAVE PROCUCT: " + fb.getExtensao());
        if (fb.getExtensao().equals("rel")) {
            saveBinaryFile(val);
        } else {
            saveReportsVerbose(fb, fb.getExtensao());
        }
    }

    public synchronized void saveClientes(Hashtable<String, Cliente> val) {
        try {
            out = new ObjectOutputStream(new FileOutputStream(fileClientes));
            out.writeObject(val);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.DA75A588-5D5C-C731-2D65-BF092496B01D]
    // </editor-fold> 
    public synchronized Hashtable<String, Cliente> loadClientes() {

        Hashtable<String, Cliente> val = new Hashtable<String, Cliente>();
        try {
            in = new ObjectInputStream(new FileInputStream(fileClientes));
            val = (Hashtable<String, Cliente>) in.readObject();
            System.out.println("Carregou clientes:" + val.size());
            in.close();
        } catch (Exception ex) {
            saveClientes(val);
        }
        return val;
    }

    public synchronized Hashtable<String, Painel> loadPainels() {

        Hashtable<String, Painel> val = new Hashtable<String, Painel>();
        try {
            in = new ObjectInputStream(new FileInputStream(panelsFile));
            val = (Hashtable<String, Painel>) in.readObject();
            in.close();
        } catch (Exception ex) {
            savePanels(val);
        }
        return val;
    }

    public synchronized void savePanels(Hashtable<String, Painel> val) {

        try {
            out = new ObjectOutputStream(new FileOutputStream(panelsFile));
            out.writeObject(val);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized Hashtable<String, Funcionario> loadFuncionarios() {

        Hashtable<String, Funcionario> val = new Hashtable<String, Funcionario>();
        try {
            in = new ObjectInputStream(new FileInputStream(fileFunc));
            val = (Hashtable<String, Funcionario>) in.readObject();
            in.close();
        } catch (Exception ex) {
            saveFuncionarios(val);
        }
        return val;
    }

    public synchronized void saveFuncionarios(Hashtable<String, Funcionario> val) {

        try {
            out = new ObjectOutputStream(new FileOutputStream(fileFunc));
            out.writeObject(val);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd#HH_mm_ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    private void saveReportsVerbose(FileProduct fb, String extension) {
        System.out.println("SAVE VERBOSE");
        Writer output = null;
        try {
            output = new BufferedWriter(new FileWriter(new File("OPTXT" + getDateTime() + "." + extension)));

            output.write(fb.getCabecalho());
            output.write("\n");
            output.write(fb.getCorpo().toString());

        } catch (IOException ex) {
            ex.printStackTrace();

        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}



